package com.wondertek.mist.plugins.takepicture;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.text.TextPaint;

/**
 *android添加水印方法
 */

public class Watermark {

    /**
     *android添加水印方法
     * @param src 原图片
     * @param title 水印文字
     * @param watermark 水印图片
     * @return
     */
    public static Bitmap addWatermarkToPicture(Bitmap src, String title, Bitmap watermark) {
        if (src == null) {
            return null;
        }
        int w = src.getWidth();
        int h = src.getHeight();
        // 处理图片太大成的内存的,我的图片很小所以不写相应代码了
        Bitmap newb = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);// 创建一个新的和SRC度宽度一样的位图
        Canvas cv = new Canvas(newb);
        cv.drawBitmap(src, 0, 0, null);// 在 00坐标开始画入src
        Paint paint = new Paint();
        // 加入图片
        if (watermark != null) {
            int ww = watermark.getWidth();
            int wh = watermark.getHeight();
            paint.setAlpha(50);
            // cv.drawBitmap(watermark, w - ww + 5, h - wh + 5, paint);//
            // 在src的右下画入水印
            cv.drawBitmap(watermark, 0, 0, paint);// 在src的左上画入水印
        } else {
            // Log.i("i", "water mark failed");
        }
        // 加入文字
        if (title != null) {
            String familyName = "微";
            Typeface font = Typeface.create(familyName, Typeface.BOLD);
            TextPaint textPaint = new TextPaint();
            ///textPaint.setColor(Color.WHITE);
            textPaint.setColor(Color.RED);
            textPaint.setAlpha(0x40);
            textPaint.setTypeface(font);
            textPaint.setTextSize(20);
            // 是动换的
            // StaticLayout layout = new
            // StaticLayout(title,textPaint,w,Alignment.ALIGN_OPPOSITE,1.0F,0.0F,true);
            // layout.draw(cv);
            if (title.length() > 26 ) {
                String watermarkStr1 = title.substring(0, 26);
                String watermarkStr2 = title.substring(26, title.length());
                cv.drawText(watermarkStr1, 10, h - 40, textPaint);
                cv.drawText(watermarkStr2, 10, h - 17, textPaint);
            } else {
                cv.drawText(title, 10, h - 30, textPaint);
            }

        }
        cv.save(Canvas.ALL_SAVE_FLAG);// 保存
        cv.restore();// 存储
        return newb;

    }

}
